$(function () {
    initNav();


    let userid = getCookie("userid");
    let token = getCookie("token");

    // 请求购物车数据; 
    function init() {

        var options = {
            url: "http://127.0.0.1:8888/cart/list",
            type: "GET",
            data: {
                id: userid
            },
            headers: {
                authorization: token
            },
            dataType: "json"
        };

        $.ajax(options)
            .done(function (res) {
                render(res);
                console.log(res);
            })
    }

    function render(res) {
        $(".cart-container").html(
            res.cart.map(item =>
                `<div class="goods">
                            <div class="p-goods">
                                  <div class="p-img">
                                        <img src="${item.img_small_logo}" alt="">
                                  </div>
                            </div>
                            <div class="p-item">
                                ${item.title}
                            </div>
                            <div class="p-price">
                                ￥${item.price}
                            </div>
                            <div class="p-quantity">
                                  <div class="cartnum">
                                        <button class="btnReduce">-</button>
                                        <input type="text" value=${item.cart_number}>
                                        <button class="btnAdd">+</button>
                                  </div>
                            </div>
                            <div class="p-sum"> ￥${item.cart_number * item.price}</div>
                            <div class="p-options">
                                  <a data-id=${item.goods_id} href="javascript:void(0)"><button>删除</button></a>
                            </div>
                      </div>`
            ).join("")
        )

    }
    init();
    console.log($('.btnReduce'));
    $('.btnReduce').click(function(){
        console.log(1);
        $(".item.cart_number")++;
    })
    $('.btnAdd').click(function(){
        $(".item.cart_number")--;
    })
});